package com.ayit.car.controller.car;

import com.ayit.car.common.result.PageResult;
import com.ayit.car.common.result.Result;
import com.ayit.car.pojo.Colors;
import com.ayit.car.pojo.dto.ColorsPageQueryDTO;
import com.ayit.car.service.ColorsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car/colors")
@Slf4j
@Api(tags = "颜色管理")
public class ColorController {

    @Autowired
    private ColorsService colorsService;

    /**
     * 添加颜色
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加颜色")
    public Result addColor(@RequestBody Colors colors) {
        log.info("添加车辆颜色信息:{}",colors);
        colorsService.save(colors);
        return Result.success();
    }

    /**
     * 查询颜色列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询颜色列表")
    public Result listColor(Integer colorType) {
        log.info("查询颜色列表");
        if (colorType == null) {
            return Result.success(colorsService.list());
        }
        QueryWrapper<Colors> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("color_type", colorType);
        List<Colors> colors = colorsService.list(QueryWrapper);
        return Result.success(colors);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询颜色列表")
    public Result<PageResult> listColor(ColorsPageQueryDTO colorsPageQueryDTO) {
        log.info("分页查询颜色列表:{}", colorsPageQueryDTO);
        PageResult pageResult = colorsService.pageQuery(colorsPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取颜色信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取颜色信息")
    public Result getInfo(@PathVariable Integer id) {
        log.info("获取颜色信息:{}", id);
        Colors color = colorsService.getById(id);
        return Result.success(color);
    }

    /**
     * 修改颜色
     * @param colors
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改颜色")
    public Result updateColor(@RequestBody Colors colors) {
        log.info("修改车辆颜色信息:{}",colors);
        colorsService.updateById(colors);
        return Result.success();
    }

    /**
     * 删除颜色
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除颜色")
    public Result deleteColor(@RequestParam List<Integer> ids) {
        log.info("删除车辆颜色信息:{}",ids);
        colorsService.removeByIds(ids);
        return Result.success();
    }
}
